<?php
include("functions.php");
include("libs/smarty.class.php");
$smarty = new Smarty();

$title = "Panel użytkownika.";
$smarty->assign("title",$title);

if(isset($_SESSION['user_id'])){
    $student_id = $_SESSION['user_id'];

    $facultyId =  $_SESSION['student_info']['0']['faculty_id'];
    $specializationId = $_SESSION['student_info']['0']['specialization_id'];

    $faculty = getRow("name", $facultyId, "faculty");
    $specialization = getRow("name", $specializationId, "specializations");
    
    $smarty -> assign("specialization",$specialization['0']['name']);
    $smarty -> assign("faculty",$faculty['0']['name']);
    
    if($_SESSION['user_type'] == 'user_student'){
        //CHANGE PASSWORD
        if($_POST['submit_change_password']){
            $oldPass = $_POST['old_password'] ;
            $newPass = $_POST['new_password'];
            $info_change = changePassword("user_student", $student_id, $oldPass, $newPass);
        }
        //CHANGE EMAIL

        if($_POST['submit_change_email']){
            $new_email = $_POST['new_email'];
            $info_change = changeEmail("user_student", $student_id, $new_email);
        }
        //CHANGE FACULTY
        $specializations = array();
        $faculties = getRows("*","faculty");
        if($_POST['id']){
            $faculty_id=$_POST['id'];
            $sql = mysql_query("SELECT s.id,s.name FROM faculty f, specializations s
                            WHERE (s.id_faculty = '$faculty_id' AND s.id_faculty=f.id)");
            while($row = mysql_fetch_assoc($sql)){
                $specializations[] = $row;
            }
        }
        if($_POST['submit_change_faculty']){
            $faculty_id = $_POST['s_faculty'];
            $specialization_id = $_POST['s_specialization'];
            $info_change = changeFaculty($student_id, $faculty_id, $specialization_id);
        }
        //CHANGE ALBUM
        if($_POST['submit_change_album']){
            $new_album = $_POST['new_album'];
            $info_change = changeAlbum("user_student", $student_id, $new_album);

        }
        //CHECK QUEUE
        $check = mysql_query("SELECT spq.id FROM student_practices_queue spq WHERE spq.student_id ='$student_id'");
        if(mysql_num_rows($check) == 0){
            //CHECK PRACTICE
            $get_practice_id = getRow("practice_id", $student_id,"user_student");
            $practice_id = $get_practice_id['0']['practice_id'];

            if($practice_id != 0){
                    $practiceDetails = getPracticeDetails($practice_id);
                    $smarty->assign("p",$practiceDetails['practice']);
                    $smarty->assign("c",$practiceDetails['company']);
                    $smarty->assign("pe",$practiceDetails['person']);
                    $smarty->assign("practiceDetails",$practiceDetails);
                }else{
                    $info_practice = getInfo(8);
                }
        }else{
            $info_practice = getInfo(4);
        }
        $smarty->assign("info_change",$info_change);
        $smarty->assign("info_practice",$info_practice);
        $smarty->assign("specializations",$specializations);
        $smarty->assign("faculties",$faculties);
        $smarty->display('user_panel_student.tpl');
    }


    if($_SESSION['user_type'] == 'user_company'){
        $smarty->display('user_panel_company.tpl');
    }
}else{
    echo "Musisz być zalogowany!";
}

?>